﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.Data;
using KPIS.GERP.GBMS.DAL;
using KPIS.GERP.GBMS.MODEL;
using KPIS.GERP.GBMS.DALFactory;
using KPIS.GERP.GBMS.IBLL;

namespace KPIS.GERP.GBMS.BLL
{
    public class SourceBLL : ISourceBLL
    {
        private static readonly ISourceDAL dal = DataAccess.CreateSource();
        private static readonly ISourceTypeDAL srcType = DataAccess.CreateSourceType();
        private SourceTypeInfo srcTypeInfo = new SourceTypeInfo();
        private SourceInfo srcParent = new SourceInfo();

        public SourceBLL()
        {

        }

        public IList<TreeViewInfo> TreeView(IList<TreeViewInfo> tView, bool isRootNode)
        {
            IList<TreeViewInfo> treeView = new List<TreeViewInfo>();
            
            if (!isRootNode)
            {
                tView = srcType.TreeView(tView);
                tView = dal.TreeView(tView, false);            
            }
            else 
            {
                tView = dal.TreeView(tView, false);
            }

            return tView;
        }

        public void Insert(string code, string name, string descr, int parent, int type, string gfmis_code)
        {
            srcTypeInfo.Sequence = type;
            srcParent.Sequence = parent;

            int chkError = dal.Insert(new SourceInfo(0, code, name, descr, "", gfmis_code, srcTypeInfo, srcParent, new UserRecordInfo(1, null)));

            if (chkError == 0)
            {
                // Error
            }
        }

        public void Update(int seq, string code, string name, string descr, int parent, int type, string gfmis_code)
        {
            srcTypeInfo.Sequence = type;
            srcParent.Sequence = parent;

            int chkError = dal.Update(new SourceInfo(seq, code, name, descr, "", gfmis_code, srcTypeInfo, srcParent, null));

            if (chkError == 0)
            {
                // Error
            }
        }

        public void Delete(int seq)
        {
            int chkError = dal.Delete(seq);
        }

        public IList<SourceInfo> Search(int seq)
        {
            return Search(seq, "", "", "", 0, 0);
        }

        public IList<SourceInfo> Search(int seq, string code, string name, string descr, int parent, int type)
        {
            srcTypeInfo.Sequence = type;
            srcParent.Sequence = parent;

            return dal.Search(new SourceInfo(seq, code, name, descr, "", "", srcTypeInfo, srcParent, null));
        }

        //public bool Search(int seq, string code, string name, string descr, int parent, int type)
        //{
        //    srcTypeInfo.Sequence = type;
        //    srcParent.Sequence = parent;

        //    return dal.Search(new SourceInfo(seq, code, name, descr, "", "", srcTypeInfo, srcParent, null));
        //}


        public bool CheckDuplicateCode(string code, string currentSeq)
        {
            return dal.CheckDuplicateCode(code, currentSeq);
        }
    }
}
